//
//  KyContentView.swift
//  KyPageViewDemo
//
//  Created by fangyukui on 2017/11/21.
//  Copyright © 2017年 fangyukui. All rights reserved.
//

import UIKit

protocol KyContentViewDelegate:class{
    func contentView(contentView:KyContentView,didTargetIdx:Int) ->Void
    func contentView(contentView:KyContentView,targetIdx:Int,progress:CGFloat) -> Void
}

private let itemId = "itemId"
class KyContentView: UIView {
    fileprivate var childVcs:[UIViewController]
    fileprivate var parentVc:UIViewController
    fileprivate lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: self.bounds.width, height: self.bounds.height)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let collectionView = UICollectionView(frame: self.bounds, collectionViewLayout: layout)
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: itemId)
        return collectionView
        
    }()
    
    weak var delegate:KyContentViewDelegate?
    
    fileprivate var startContentoffset : CGPoint = .zero
    
    init(frame: CGRect,childVcs:[UIViewController],parentVc:UIViewController) {
        self.childVcs = childVcs
        self.parentVc = parentVc
        super.init(frame: frame)
        setupUI()
    
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

}
extension KyContentView{
    fileprivate func setupUI(){
        for childVc  in childVcs {
            parentVc.addChildViewController(childVc)
            
        }
        addSubview(collectionView)
        
    }
}
extension KyContentView:UICollectionViewDataSource,UICollectionViewDelegate{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
        
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return childVcs.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item =  collectionView.dequeueReusableCell(withReuseIdentifier: itemId, for: indexPath)
        for subView in item.contentView.subviews{
            subView.removeFromSuperview()
        }
        let childVc = childVcs[indexPath.row]
        childVc.view.frame = item.bounds
        item.addSubview(childVc.view)
        return item
    }
    //减速
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let idx = Int(scrollView.contentOffset.x / bounds.width)
        delegate?.contentView(contentView: self, didTargetIdx: idx)
        
    }
    //拖曳
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if !decelerate {
            let idx = Int(scrollView.contentOffset.x / bounds.width)
            delegate?.contentView(contentView: self, didTargetIdx: idx)
        }
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        startContentoffset = scrollView.contentOffset
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard scrollView.contentOffset.x != startContentoffset.x else {
            return
        }
        let currentIdx = Int(startContentoffset.x / bounds.width)
        
        let progress = (scrollView.contentOffset.x - startContentoffset.x) / bounds.width
        
        if scrollView.contentOffset.x > startContentoffset.x { //左滑
            
            let targetIdx = (currentIdx + 1) > (childVcs.count - 1) ? currentIdx : currentIdx + 1
            delegate?.contentView(contentView: self, targetIdx: targetIdx, progress: progress)
            
            
        }else{ //右滑
            let targetIdx = currentIdx -  1  < 0 ? 0 : currentIdx -  1
            delegate?.contentView(contentView: self, targetIdx: targetIdx, progress: -progress)
            
        }
    }
    
    
    
    
}
extension KyContentView:KyTitleViewDelegate{
    func titleView(titleView: KyTitleView, targetIdx: NSInteger) {
        let idxP = IndexPath(item: targetIdx, section: 0)
        collectionView.scrollToItem(at: idxP, at: .left, animated: true)
    }
}
